package com.pw.lit.controller;

import com.pw.lit.bean.*;
import com.pw.lit.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/Admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private DiaryService diaryService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private ReplyService replyService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private LabelsService labelsService;

    @RequestMapping("/AIndex")
    public String AIndex(Model model, HttpSession session) {
        model.addAttribute("em", "1");
        session.removeAttribute("msg");
        return "ServicePages/aindex";
    }

    //用户列表
    @RequestMapping("/userlist")
    @ResponseBody
    public Map<String, Object> userlist(@RequestParam("limit") String limit,
                                        @RequestParam("page") String page) {
        Map<String, Object> map = new HashMap<>();
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        map.put("code", 0);
        map.put("msg", "用户列表");
        map.put("count", userService.countByExample(example));
        map.put("data", pages(userService.selectByExample(example), Integer.parseInt(limit), Integer.parseInt(page)));
        return map;
    }

    //博客列表
    @RequestMapping("/bloglist")
    @ResponseBody
    public Map<String, Object> bloglist(@RequestParam("limit") String limit,
                                        @RequestParam("page") String page) {
        Map<String, Object> map = new HashMap<>();
        BlogExample example = new BlogExample();
        BlogExample.Criteria criteria = example.createCriteria();
        criteria.andBidIsNotNull();
        map.put("code", 0);
        map.put("msg", "博客列表");
        map.put("count", blogService.countByExample(example));
        map.put("data", pages(blogService.selectByExample(example), Integer.parseInt(limit), Integer.parseInt(page)));
        return map;
    }

    //日记列表
    @RequestMapping("/diarylist")
    @ResponseBody
    public Map<String, Object> diarylist(@RequestParam("limit") String limit,
                                         @RequestParam("page") String page) {
        Map<String, Object> map = new HashMap<>();
        DiaryExample example = new DiaryExample();
        DiaryExample.Criteria criteria = example.createCriteria();
        criteria.andDidIsNotNull();
        map.put("code", 0);
        map.put("msg", "日记列表");
        map.put("count", diaryService.countByExample(example));
        map.put("data", pages(diaryService.selectByExample(example), Integer.parseInt(limit), Integer.parseInt(page)));
        return map;
    }

    //评论列表
    @RequestMapping("/commentlist")
    @ResponseBody
    public Map<String, Object> commentlist(@RequestParam("limit") String limit,
                                           @RequestParam("page") String page) {
        Map<String, Object> map = new HashMap<>();
        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();
        criteria.andCidIsNotNull();
        map.put("code", 0);
        map.put("msg", "日记列表");
        map.put("count", commentService.countByExample(example));
        map.put("data", pages(commentService.selectByExample(example), Integer.parseInt(limit), Integer.parseInt(page)));
        return map;
    }

    //回复列表
    @RequestMapping("/replylist")
    @ResponseBody
    public Map<String, Object> replylist(@RequestParam("limit") String limit,
                                         @RequestParam("page") String page) {
        Map<String, Object> map = new HashMap<>();
        ReplyExample example = new ReplyExample();
        ReplyExample.Criteria criteria = example.createCriteria();
        criteria.andRidIsNotNull();
        map.put("code", 0);
        map.put("msg", "回复列表");
        map.put("count", replyService.countByExample(example));
        map.put("data", pages(replyService.selectByExample(example), Integer.parseInt(limit), Integer.parseInt(page)));
        return map;
    }

    //消息列表
    @RequestMapping("/messagelist")
    @ResponseBody
    public Map<String, Object> messagelist(@RequestParam("limit") String limit,
                                           @RequestParam("page") String page) {
        Map<String, Object> map = new HashMap<>();
        MessageExample example = new MessageExample();
        MessageExample.Criteria criteria = example.createCriteria();
        criteria.andMidIsNotNull();
        map.put("code", 0);
        map.put("msg", "回复列表");
        map.put("count", messageService.countByExample(example));
        map.put("data", pages(messageService.selectByExample(example), Integer.parseInt(limit), Integer.parseInt(page)));
        return map;
    }

    //删除数据
    @RequestMapping("/deldata/{type}/{id}")
    @ResponseBody
    public Map<String, Object> deldata(@PathVariable("type") String type,
                                       @PathVariable("id") String id) {
        Map<String, Object> map = new HashMap<>();
        switch (type) {
            case "1":
                userService.deleteByPrimaryKey(Integer.parseInt(id));
                map.put("msg", "删除成功!");
                break;
            case "2":
                blogService.deleteByPrimaryKey(Integer.parseInt(id));
                map.put("msg", "删除成功!");
                break;
            case "3":
                diaryService.deleteByPrimaryKey(Integer.parseInt(id));
                map.put("msg", "删除成功!");
                break;
            case "4":
                commentService.deleteByPrimaryKey(Integer.parseInt(id));
                map.put("msg", "删除成功!");
                break;
            case "5":
                replyService.deleteByPrimaryKey(Integer.parseInt(id));
                map.put("msg", "删除成功!");
                break;
            default:
                map.put("msg", "删除失败!");
                break;
        }
        return map;
    }

    //分页方法
    public <E> List<E> pages(List<E> list, int limit, int start) {
        System.out.println(limit + "分页" + start + "总数" + list.size());
        List<E> nowList = new ArrayList<E>();
        int num = limit * start;
        if (num - limit <= list.size()) {
            for (int i = num - limit; i < list.size() && i < num; i++) {
                nowList.add(list.get(i));
            }
        } else {
            for (int i = start - 1; i < list.size(); i++) {
                nowList.add(list.get(i));
            }
        }
        return nowList;
    }

    @RequestMapping("/getdata/{type}")
    @ResponseBody
    public Map<String, Object> getdata(@PathVariable("type") String type) {
        Map<String, Object> map = new HashMap<>();
        switch (type) {
            case "1":
                map = blogdate();
                break;
            case "2":
                map = labeldata();
                break;
            case "3":
                map = diarydate();
                break;
            case "4":
                map = commentdata();
                break;
            case "5":
                map = followdata();
                break;
            case "6":
                map = fandata();
                break;
            default:
                break;
        }
        return map;
    }

    public Map<String, Object> blogdate() {
        Map<String, Object> map = new HashMap<>();
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        List<User> users = userService.selectByExample(example);
        List<String> xAxis = new ArrayList<>();
        List<String> series = new ArrayList<>();
        for (User u : users) {
            xAxis.add(u.getName());
            series.add(u.getBlogNumber().toString());
        }
        map.put("title", "用户发布博客统计");
        map.put("legend", "数量");
        map.put("xAxis", xAxis);
        map.put("series", series);
        return map;
    }

    public Map<String, Object> labeldata() {
        Map<String, Object> map = new HashMap<>();
        LabelsExample example = new LabelsExample();
        LabelsExample.Criteria criteria = example.createCriteria();
        criteria.andLabelIsNotNull();
        List<Labels> labels = labelsService.selectByExample(example);
        Set<String> xAxis = new HashSet<>();
        List<String> series = new ArrayList<>();
        for (Labels l : labels) {
            xAxis.add(l.getLabel());
        }
        for (String c : xAxis) {
            LabelsExample example1 = new LabelsExample();
            LabelsExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andLabelEqualTo(c);
            int num = labelsService.countByExample(example1);
            series.add(String.valueOf(num));
        }
        map.put("title", "博客标签统计");
        map.put("legend", "数量");
        map.put("xAxis", xAxis);
        map.put("series", series);
        return map;
    }

    public Map<String, Object> diarydate() {
        Map<String, Object> map = new HashMap<>();
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        List<User> users = userService.selectByExample(example);
        List<String> xAxis = new ArrayList<>();
        List<String> series = new ArrayList<>();
        for (User u : users) {
            xAxis.add(u.getName());
            DiaryExample example1 = new DiaryExample();
            DiaryExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andIdEqualTo(u.getId());
            int num = diaryService.countByExample(example1);
            series.add(String.valueOf(num));
        }
        map.put("title", "用户发布日记统计");
        map.put("legend", "数量");
        map.put("xAxis", xAxis);
        map.put("series", series);
        return map;
    }

    public Map<String, Object> commentdata() {
        Map<String, Object> map = new HashMap<>();
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        List<User> users = userService.selectByExample(example);
        Set<String> xAxis = new HashSet<>();
        List<String> series = new ArrayList<>();
        for (User u : users) {
            xAxis.add(u.getName());
            CommentExample example1 = new CommentExample();
            CommentExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andIdEqualTo(u.getId());
            int num = commentService.countByExample(example1);
            series.add(String.valueOf(num));
        }
        map.put("title", "评论统计");
        map.put("legend", "数量");
        map.put("xAxis", xAxis);
        map.put("series", series);
        return map;
    }

    public Map<String, Object> followdata() {
        Map<String, Object> map = new HashMap<>();
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        List<User> users = userService.selectByExample(example);
        List<String> xAxis = new ArrayList<>();
        List<String> series = new ArrayList<>();
        for (User u : users) {
            xAxis.add(u.getName());
            series.add(u.getFollows().toString());
        }
        map.put("title", "关注统计");
        map.put("legend", "数量");
        map.put("xAxis", xAxis);
        map.put("series", series);
        return map;
    }

    public Map<String, Object> fandata() {
        Map<String, Object> map = new HashMap<>();
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        List<User> users = userService.selectByExample(example);
        List<String> xAxis = new ArrayList<>();
        List<String> series = new ArrayList<>();
        for (User u : users) {
            xAxis.add(u.getName());
            series.add(u.getFans().toString());
        }
        map.put("title", "粉丝统计");
        map.put("legend", "数量");
        map.put("xAxis", xAxis);
        map.put("series", series);
        return map;
    }
}
